//
//  LGInspectDetailLRController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/3.
//

#import "LGInspectDetailLRController.h"
//检查项趋势图
#import "LGInspectChartController.h"
#import "LGGetWebViewController.h"
#import "LGReportDetailHeaderView.h"
#import "LGReportDetailHeaderCell.h"
#import "LGReportDetailLRCell.h"
#import "LGReportDetailFileCell.h"
#define viewHeight  (Screen_H-topBarHeight-viewPix(120))
@interface LGInspectDetailLRController ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)LGReportDetailHeaderView *headerView;
/**   */
@property (nonatomic , strong)UITableView *tableView;



@end

@implementation LGInspectDetailLRController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
}

-(void)setFileArray:(NSArray *)fileArray{
    _fileArray = fileArray;
    [self.tableView reloadData];
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    [self.tableView reloadData];
}

-(void)checkFileInformation:(UIButton *)sender{
    NSInteger index = sender.tag-1000;
    LGGetWebViewController *controller = [[LGGetWebViewController alloc]init];
    controller.navigationView.title = @"检查报告";
    controller.urlStr = LGNSString(self.fileArray[index]);
    [self.navigationController pushViewController:controller animated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return self.dataArray.count+1;
    }else{
        return self.fileArray.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        if(indexPath.row == 0){
            LGReportDetailHeaderCell *headCell = [tableView dequeueReusableCellWithIdentifier:@"headCell" forIndexPath:indexPath];
            headCell.selectionStyle = UITableViewCellSelectionStyleNone;
            return headCell;
            
        }else{
            LGReportDetailLRCell *lrCell = [tableView dequeueReusableCellWithIdentifier:@"lrCell" forIndexPath:indexPath];
            lrCell.selectionStyle = UITableViewCellSelectionStyleNone;
            lrCell.dataDic = self.dataArray[indexPath.row-1];
            return lrCell;
        }
    }else{
        LGReportDetailFileCell *fileCell = [tableView dequeueReusableCellWithIdentifier:@"fileCell" forIndexPath:indexPath];
        fileCell.selectionStyle = UITableViewCellSelectionStyleNone;
        fileCell.title = [NSString stringWithFormat:@"%@.pdf",self.reportName];
        fileCell.checkBtn.tag = indexPath.row+1000;
        [fileCell.checkBtn addTarget:self action:@selector(checkFileInformation:) forControlEvents:UIControlEventTouchUpInside];
        return fileCell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0 && indexPath.row > 0){
        LGInspectChartController *controller = [[LGInspectChartController alloc]init];
        controller.code = LGNSString(self.dataArray[indexPath.row-1][@"item_code"]);
        controller.patientNum = self.patientNum;
        controller.patientid = self.patientid;
        [self.navigationController pushViewController:controller animated:YES];
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(50);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGCornerHeaderView *headView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(50))];
    headView.backgroundColor = [UIColor clearColor];
    headView.topMargin = viewPix(8);
    NSString *title = (section == 0)?@"报告信息":@"报告原文";
    UIButton *titleBtn = [UIButton buttonWithTitle:title titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#1F6CC6" imageName:@""];
    titleBtn.frame = CGRectMake(viewPix(16), viewPix(20), 112, 27);
    [titleBtn setBackgroundImage:[UIImage imageNamed:@"就诊检查-蓝色"] forState:UIControlStateNormal];
    [headView addSubview:titleBtn];
    return headView;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(16);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(16))];
    footView.backgroundColor = [UIColor clearColor];
    footView.bottomMargin = viewPix(8);
    return footView;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGReportDetailHeaderCell class] forCellReuseIdentifier:@"headCell"];
        [_tableView registerClass:[LGReportDetailLRCell class] forCellReuseIdentifier:@"lrCell"];
        [_tableView registerClass:[LGReportDetailFileCell class] forCellReuseIdentifier:@"fileCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    }
    return _tableView;
}


@end
